package dataStructure.chapter02_动态数组.part1整数型数组;

/**
 * @author https://gitee.com/W_Jun
 * @date 2022-01-14 17:30
 * @Description 自己实现动态数组，简单起见，存储的数据规定是int类型
 */
public class MyIntArrayList {
    //元素的数量
    private int size;

    //默认容量
    private static final int DEFAULT_CAPACITY = 10;

    //所有的元素
    private int[] elements;

    /**
    * 构造器-有参
    * */
    public MyIntArrayList(int capacity) {
        capacity = (capacity < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY :capacity;
        elements = new int[capacity];
    }

    /**
    * 构造器-无参
    * */
    public MyIntArrayList() {
        this(DEFAULT_CAPACITY);
    }

    /**
    * 清除所有元素
    * */
    public void clear() {
        size = 0;
    }

    /**
    * 数组中元素的数量
    * @return int
    * */
    public int size() {
        return size;
    }

    /**
    * 判断数组是否为空
    * @return boolean
    * */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
    * 是否包含某个元素
    * @params element
    * @return boolean
    * */
    public boolean contains(int element) {
        return indexOf(element) != -1;
    }

    /**
    * 添加元素到数组尾部
    * @param element
    * */
    public void add(int element) {
        add(size, element);
    }

    /**
    * 获取index索引位置的元素
    * @param index
    * @return int
    * */
    public int get(int index) {
        rangeCheck(index);
        return elements[index];
    }

    /**
    * 设置index位置的元素，返回原来的元素
    * @param index
    * @param element
    * @return int
    * */
    public int set(int index, int element) {
        rangeCheck(index);
        int old = elements[index];
        elements[index] = element;
        return old;
    }

    /**
    * 在index位置插入一个元素
    * @param index
    * @param element
    * */
    public void add(int index, int element) {
        rangeCheckForAdd(index);
        ensureCapacity(size + 1);
        for (int i = size - 1; i >= index; i--) {
            elements[i + 1] = elements[i];
        }
        elements[index] = element;
        size++;
    }

    /**
    * 删除index位置的元素，返回被删除元素
    * @param index
    * @return int
    * */
    public int remove(int index) {
        rangeCheck(index);
        int old = elements[index];
        for (int i = index; i < size; i++) {
            elements[i] = elements[i + 1];
        }
        size--;
        return old;
    }

    /**
    * 查看元素的索引
    * @param element
    * @return int
    * */
    public int indexOf(int element) {
        for (int i = 0; i < size; i++) {
            if (elements[i] == element) return i;
        }
        return -1;
    }

    /**
    * 检测index的范围，对set、get、remove方法
    * */
    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

    /**
    * 检测index的范围，对于add方法
    * */
    private void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }

    /**
    * index范围超出，报异常信息
    * */
    private void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("index：" + index + ", size:" + size);
    }

    /**
    * 保证要有capacity的容量,不满足就扩容
     * @param capacity
    * */
    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (oldCapacity >= capacity) return;
        int newCapacity =oldCapacity + (oldCapacity >> 1);
        int[] newElements = new int[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
    }

    /**
    * 为了测试方便，重写toString方法。注意java api实现的ArrayList是没有重写toString的
    * */
    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(", [");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }
            string.append((elements[i]));
        }
        string.append("]");
        return string.toString();
    }
}
